import Icon from '@/components/Icon'
import { addChannel, delChannels } from '@/store/actions/home'
import classNames from 'classnames'
import { useState } from 'react'
import { useDispatch, useSelector } from 'react-redux'
import styles from './index.module.scss'
import { Toast } from 'antd-mobile'

/**
 * 频道管理组件
 * @param {Number} props.tabActiveIndex 用户选中的频道的索引
 * @param {Function} props.onClose 关闭频道管理抽屉时的回调函数
 * @param {Function} props.onChannelClick 当点击频道列表中的某个频道时的会带哦函数
 */
const Channels = ({ index, onClose, onChange }) => {
  const userChannels = useSelector((state) => state.home.userChannels)
  const recommendChannels = useSelector((state) => {
    const { allUserChannelList, userChannels } = state.home
    return allUserChannelList.filter((item) => {
      // return userChannels.findIndex((o) => o.id === item.id) === -1
      return !userChannels.some((o) => o.id === item.id)
    })
  })
  // console.log(recommendChannels)

  // 控制编辑模式的切换
  const [editTab, setEditTab] = useState(false)

  // 删除频道
  const dispatch = useDispatch()
  const deleteChannel = async (item, i) => {
    if (userChannels.length <= 4) {
      return Toast.info('至少要保留四个')
    }
    await dispatch(delChannels(item.id))
    Toast.success('删除成功')

    // 处理高亮
    if (index === i) {
      onChange(0)
    }
    if (i < index) {
      onChange(index - 1)
    }
  }

  // 添加频道
  const addChannels = async (channel) => {
    await dispatch(addChannel(channel))
    Toast.success('添加成功', 1)
  }

  return (
    <div className={styles.root}>
      {/* 顶部栏：带关闭按钮 */}
      <div className="channel-header">
        <Icon type="iconbtn_channel_close" onClick={onClose} />
      </div>

      {/* 频道列表 */}
      <div className="channel-content">
        {/* 当前已选择的频道列表 */}
        <div className={classNames('channel-item', editTab ? 'edit' : '')}>
          <div className="channel-item-header">
            <span className="channel-item-title">我的频道</span>
            <span className="channel-item-title-extra">
              {editTab ? '点击删除频道' : '点击进入频道'}
            </span>
            <span
              className="channel-item-edit"
              onClick={() => setEditTab(!editTab)}
            >
              {editTab ? '保存' : '编辑'}
            </span>
          </div>

          <div className="channel-list">
            {userChannels.map((item, i) => (
              <span
                key={item.id}
                className={classNames('channel-list-item', {
                  selected: i === index,
                })}
                onClick={() => {
                  if (!editTab) {
                    onChange(i)
                    onClose()
                  }
                }}
              >
                {item.name}
                {i === 0 ? (
                  ''
                ) : (
                  <Icon
                    type="iconbtn_tag_close"
                    onClick={() => deleteChannel(item, i)}
                  />
                )}
              </span>
            ))}
          </div>
        </div>

        {/* 推荐的频道列表 */}
        <div className="channel-item">
          <div className="channel-item-header">
            <span className="channel-item-title">频道推荐</span>
            <span className="channel-item-title-extra">点击添加频道</span>
          </div>
          <div className="channel-list">
            {recommendChannels.map((item) => (
              <span
                key={item.id}
                className="channel-list-item"
                onClick={() => addChannels(item)}
              >
                {item.name}
              </span>
            ))}
          </div>
        </div>
      </div>
    </div>
  )
}

export default Channels
